from urllib import request
import json

base_url = 'https://xueqiu.com/stock/quote_order.json?page={}&size=30&order=desc&exchange=CN&stockType=sha&column=symbol%2Cname%2Ccurrent%2Cchg%2Cpercent%2Clast_close%2Copen%2Chigh%2Clow%2Cvolume%2Camount%2Cmarket_capital%2Cpe_ttm%2Chigh52w%2Clow52w%2Chasexist&orderBy=percent&_=1528683544364'
f = open('stock.json','w',encoding='utf-8')

headers = {
    # 'Host': 'xueqiu.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
    'Cookie' : 'device_id=04f1ecffa970be09c8c76f4835bf4871; s=f812mu6du8; xq_a_token=019174f18bf425d22c8e965e48243d9fcfbd2cc0; xq_a_token.sig=_pB0kKy3fV9fvtvkOzxduQTrp7E; xq_r_token=2d465aa5d312fbe8d88b4e7de81e1e915de7989a; xq_r_token.sig=lOCElS5ycgbih9P-Ny3cohQ-FSA; Hm_lvt_1db88642e346389874251b5a1eded6e3=1528683462; u=481528683462708; _ga=GA1.2.1871476164.1528683463; _gid=GA1.2.1390815972.1528683463; aliyungf_tc=AQAAAAnbbjG2NA8AM09FeWB0IPkpqX79; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1528683472; __utma=1.958553154.1515596801.1525312444.1528683472.7; __utmc=1; __utmz=1.1528683472.7.4.utmcsr=xueqiu.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utmt=1; __utmb=1.1.10.1528683472'
}

for i in range(1,45 + 1):
    fullurl = base_url.format(i)
    print(fullurl)
    req = request.Request(fullurl,headers=headers)
    response = request.urlopen(req)
    data = response.read().decode('utf-8')
    data = json.loads(data)
    for stock in data['data']:
        f.write(json.dumps(stock,ensure_ascii=False) + ',\n')

f.close()